using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;
using System.Xml;

namespace Pampa.CRUD.Configuration
{
    public abstract class EntityAbstractItem : EditItem
    {
        string entityName;
        string parentId;
        string foreingProperty;

        public EntityAbstractItem() : base()
        { 
        }

        public EntityAbstractItem(EntityAbstractItem sourceItem) : base(sourceItem)
        {
            this.EntityName = sourceItem.EntityName;
            this.ParentId = sourceItem.ParentId;
            this.ForeingProperty = sourceItem.ForeingProperty;
        }

        #region Properties

        public string EntityName
        {
            get { return entityName; }
            set { entityName = value; }
        }

        /// <summary>
        /// Id del combo padre. Esta propiedad permite enlazar los combos.
        /// </summary>
        public string ParentId
        {
            get { return parentId; }
            set { parentId = value; }
        }

        /// <summary>
        /// Propiedad que debera matchear con el valor del combo padre.
        /// </summary>
        public string ForeingProperty
        {
            get { return foreingProperty; }
            set { foreingProperty = value; }
        }

        public string PropertyId
        {
            get;
            set;
        }

        public string PropertyDescription
        {
            get;
            set;
        }

        public override Type ValueTypeDefault
        {
            get { return Type.GetType(this.EntityName); }
        }

        #endregion

        #region Xml

        public override void LoadXml(XmlNode node)
        {
            try
            {
                base.LoadXml(node);

                this.EntityName = node.Attributes["EntityName"].Value;

                if (node.Attributes["ParentId"] != null)
                    this.ParentId = node.Attributes["ParentId"].Value;

                if (node.Attributes["ForeingProperty"] != null)
                    this.ForeingProperty = node.Attributes["ForeingProperty"].Value;
            }
            catch (Exception ex)
            {
                throw new Pampa.CRUD.Exceptions.ConfigurationException("Item " + this.Id + " is not well defined.", ex);
            }
        }

        #endregion
    }
}
